#pragma once
#include <iostream>
#include <unordered_map>
#include "epoll.hpp"
#include "sock.hpp"
#include "funtion.hpp"
#include "data.hpp"
using namespace std;
#define EPOLL_NUM 100
class EpollSever
{
public:
  EpollSever()
      : _sever(&_sock)
  {
    _sever.Bind(8080, "0.0.0.0");
    _sever.Listen();
    Data<std::string>* ptr = new Data<std::string>;
    ptr->_epfd = this;
    ptr->_sock = &_sever;
    ptr->_eptr = &_epfd;
    ptr->_fd = _sock;
    _map.emplace(make_pair(_sock, ptr));
    _epfd.Epoll_ctl(EPOLL_CTL_ADD, _sock, EPOLLIN | EPOLLET);
  }
  void Start()
  {
    while (true)
    {
      int num = _epfd.Epoll_wait(_arr, EPOLL_NUM, 1000);
      switch (num)
      {
      case 0:
        cout << "timeout" << endl;
        break;
      case -1:
        cout << "Epoll_Wait fail" << endl;
        break;
      default:
        _func.Handle(num,_sock, _map, _arr);
        break;
      }
    }
  }
  ~EpollSever()
  {
  }

private:
  int _sock;
  Sock _sever;
  Epoll _epfd;
  epoll_event _arr[EPOLL_NUM];
  Funtion _func;
  unordered_map<int, Data<std::string> *> _map;
};
